import { message } from "antd";
import * as authAdminService from '../services/admin';

export default {
  namespace: 'authAdmin',

  state: {
    data: {
      total: 0,
      size: 10,
      current: 1,
      pages: 1,
      records: []
    },
    searchValues: {}
  },

  effects: {
    *page({ payload }, { call, put }) {
      console.log("page authAdmins");
      console.log(payload);
      const res = yield call(authAdminService.page, payload);
      if (res.code === 200) {
        yield put({
          type: 'saveData',
          payload: res.data,
        });
      }
    },
    *get({ payload }, { call, put }) {
      console.log("get authAdmin");
      console.log(payload);
      const res = yield call(authAdminService.get, payload);
      if (res.code === 200) {
        yield put({
          type: 'test',
          payload: res.data,
        });
      }
    },
    *add({ payload }, { call, put, select }) {
      console.log("add authAdmin");
      console.log(payload);
      const res = yield call(authAdminService.add, payload);
      const data = yield select(state => state.authAdmin.data)
      if (res.code === 201) {
        message.success('添加成功');
        yield put({
          type: 'page',
          payload: {
            pageIndex: data.current,
            pageSize: data.size,
            ...data.searchValues
          },
        });
      }

    },
    *update({ payload }, { call, put, select }) {
      console.log("update authAdmin");
      console.log(payload);
      const res = yield call(authAdminService.update, payload);
      const data = yield select(state => state.authAdmin.data)
      if (res.code === 201) {
        message.success('编辑成功');
        yield put({
          type: 'page',
          payload: {
            pageIndex: data.current,
            pageSize: data.size,
            ...data.searchValues
          },
        });
      }
    },
    *del({ payload }, { call, put, select }) {
      console.log("delete authAdmin");
      console.log(payload);
      const res = yield call(authAdminService.del, payload);
      const data = yield select(state => state.authAdmin.data)
      if (res.code === 204) {
        message.success('删除成功');
        if (data.total % data.size === 1 && data.current > 1) {
          yield put({
            type: 'page',
            payload: {
              pageIndex: data.current - 1,
              pageSize: data.size,
              ...data.searchValues
            },
          });
          return;
        }
        yield put({
          type: 'page',
          payload: {
            pageIndex: data.current,
            pageSize: data.size,
            ...data.searchValues
          },
        });
      }
    },
    *delBatch({ payload }, { call, put, select }) {
      console.log("deleteBatch authAdmin");
      console.log(payload);
      const res = yield call(authAdminService.delBatch, payload);
      const data = yield select(state => state.authAdmin.data)
      if (res.code === 204) {
        message.success('删除成功');
        if (data.total % data.size === payload.length && data.current > 1) {
          yield put({
            type: 'page',
            payload: {
              pageIndex: data.current - 1,
              pageSize: data.size,
              ...data.searchValues
            },
          });
          return;
        }
        yield put({
          type: 'page',
          payload: {
            pageIndex: data.current,
            pageSize: data.size,
            ...data.searchValues
          },
        });
      }
    },
    *updateSearch({ payload }, { put }) {
      console.log("deleteBatch authAdmin");
      console.log(payload);
      yield put({
        type: 'updateSearchValue',
        payload: {
          ...payload
        }
      });
    }
  },

  reducers: {
    saveData(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    updateSearchValue(state, action) {
      return {
        ...state,
        searchValues: action.payload,
      };
    }
  },
};
